/**
 * @param {number} n
 * @return {string}
 */
var countAndSay = function (n) {
  let curNum = '1'
  if (n === 1) {
    return '1'
  }
  let cn = n - 1
  let next = ''
  const getCurrentNum = cur => {
    let left = 0
    let right = 0
    let time = 0
    while (left <= curNum.length - 1) {
      if (curNum.charAt(left) === curNum.charAt(right)) {
        time++
        right++
      } else {
        next = next + time + curNum.charAt(left)
        left = right
        time = 0
      }
    }
    return next
  }
  while (cn !== 0) {
    let nextNum = getCurrentNum(curNum)
    curNum = next
    next = ''
    cn--
  }
  return curNum
}

console.log(countAndSay(4))
